﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Assets.Script.Config;
using System;
using PathologicalGames;

public class BattleEffectMgr :Singleton<BattleEffectMgr>
{
    private int m_iInitId = 0;
    //用list 因为update 会遍历刷
    private List<BattleEffectObj> m_lisAllEffect= new List<BattleEffectObj>();

    //某个实体 所携带的特效
    private Dictionary<int, EntityContainer> m_lisEntityEffect = new Dictionary<int, EntityContainer>();

    //战斗类型
    private HeroBattleStage.BattleBigType m_bigType;

    public class EntityContainer 
    {
        public int m_iEntityId;
        public List<BattleEffectObj> m_lisEffs;
        public EntityContainer(int id)
        {
            m_iEntityId = id;
            m_lisEffs = new List<BattleEffectObj>();
        }
    }

    public void Init(HeroBattleStage.BattleBigType batType)
    {
        m_bigType = batType;
    }

    public BattleEffectObj CreateEffectObj(int pId)
    {
        HeroEff config = ConfigManager.Instance.heroEffLoader.GetInfoById(pId);
        if (config == null)
        {
            Debug.Log(pId + "  is nil");
            return null;
        }
            
        BattleEffectObj obj = new BattleEffectObj();
        obj.OnCreate(m_iInitId, config);
        m_lisAllEffect.Add(obj);
        m_iInitId++;

        return obj;
    }

    public BattleEffectObj CreateEffectObj(int pId,Vector3 pos)
    {
        BattleEffectObj obj = CreateEffectObj(pId);
        if (obj == null)
            return null;

        obj.m_pGameobj.transform.position = pos;
        return obj;
    }

    public BattleEffectObj CreateEffectObj(int pId,Transform parent,Vector3 localPos)
    {
        BattleEffectObj obj = CreateEffectObj(pId);
        if (obj == null)
            return null;

        obj.m_pGameobj.transform.SetParent(parent);
        obj.m_pGameobj.transform.localPosition = localPos;
        return obj;
    }

    public BattleEffectObj CreateEffectObj(int pId, Transform parent, Vector3 localPos, Vector3 localAngles)
    {
        BattleEffectObj obj = CreateEffectObj(pId);
        if (obj == null)
            return null;

        obj.m_pGameobj.transform.SetParent(parent);
        obj.m_pGameobj.transform.localPosition = localPos;
        obj.m_pGameobj.transform.localEulerAngles = localAngles;
        return obj;
    }

    public BattleEffectObj CreateEffectObj(int pId, int entityId)
    {
        BattleEffectObj obj = CreateEffectObj(pId);
        if (obj == null)
            return null;

        if(!m_lisEntityEffect.ContainsKey(entityId))
        {
            m_lisEntityEffect[entityId] = new EntityContainer(entityId);
        }
        m_lisEntityEffect[entityId].m_lisEffs.Add(obj);

        return obj;
    }

    public BattleEffectObj CreateEffectObj(int pId, int entityId,Vector3 pos)
    {
        BattleEffectObj obj = CreateEffectObj(pId, entityId);
        if (obj == null)
            return null;

        obj.m_pGameobj.transform.localPosition = pos;
        return obj;
    }

    public BattleEffectObj CreateEffectObj(int pId,int entityId,Transform parent,Vector3 localPos)
    {
        BattleEffectObj obj = CreateEffectObj(pId,entityId);
        if (obj == null)
            return null;

        obj.m_pGameobj.transform.SetParent(parent);
        obj.m_pGameobj.transform.localPosition = localPos;
        return obj;
    }

    public BattleEffectObj CreateEffectObj(int pId, int entityId, Transform parent, Vector3 localPos, Vector3 localAngles)
    {
        BattleEffectObj obj = CreateEffectObj(pId, entityId);
        if (obj == null)
            return null;

        obj.m_pGameobj.transform.SetParent(parent);
        obj.m_pGameobj.transform.localPosition = localPos;
        obj.m_pGameobj.transform.localEulerAngles = localAngles;
        return obj;
    }

    public GameObject GetEffRes(string path)
    {
        if(m_bigType == HeroBattleStage.BattleBigType.Hero)
        {
            if (HeroBattleMgr.Instance.m_DicEffect.ContainsKey(path))
            {
                return HeroBattleMgr.Instance.m_DicEffect[path];
            }
            else
            {
                Debug.LogFormat("{0}  is  not load", path);
            }
        }
        else if(m_bigType == HeroBattleStage.BattleBigType.Slg)
        {
            if (SlgBattleMgr.Instance.m_DicEffect.ContainsKey(path))
            {
                return SlgBattleMgr.Instance.m_DicEffect[path];
            }
            else
            {
                Debug.LogFormat("{0}  is  not load", path);
            }
        }
        return null;
    }

    public GameObject GetCloneEffRes(string path)
    {
        if (m_bigType == HeroBattleStage.BattleBigType.Hero)
        {
            if (HeroBattleMgr.Instance.m_DicEffect.ContainsKey(path))
            {
                return GameObject.Instantiate<GameObject>(HeroBattleMgr.Instance.m_DicEffect[path]);
            }
        }
        else if (m_bigType == HeroBattleStage.BattleBigType.Slg)
        {
            string prefabName = SlgFightUtil.SubPath2ObjName(path);
            //Transform obj = PoolManager.Pools[SlgFightDef.c_strEffPoolName].Spawn(SlgFightUtil.SubPath2ObjName(path));
            if (PoolManager.Pools[SlgFightDef.c_strEffPoolName].IsContainsPrefab(prefabName))
            {
                    //Debug.Log("池中取出特效");
                return PoolManager.Pools[SlgFightDef.c_strEffPoolName].Spawn(prefabName).gameObject;
            }
            else
            {
                if (SlgBattleMgr.Instance.m_DicEffect.ContainsKey(path))
                {
                    return GameObject.Instantiate<GameObject>(SlgBattleMgr.Instance.m_DicEffect[path]);
                }
            }
        }
        return null;
    }


    public float GetTimeSpeed()
    {
        if (m_bigType == HeroBattleStage.BattleBigType.Hero)
        {
            return HeroBattleMgr.Instance.Speed;
        }
        else if (m_bigType == HeroBattleStage.BattleBigType.Slg)
        {
            return SlgBattleMgr.Instance.Speed;
        }
        return 1;
    }

    public void AddTimer(Action action, float delay, string toString = "")
    {
        if (m_bigType == HeroBattleStage.BattleBigType.Hero)
        {
            HeroBattleMgr.Instance.AddTimer(action, delay, toString);
        }
        else if (m_bigType == HeroBattleStage.BattleBigType.Slg)
        {
            SlgBattleMgr.Instance.AddTimer(action, delay, toString);
        }
    }

    //删除某个特效对象
    public void DelEffectObj(int id,bool checkEntity = true)
    {
        for (int i = 0; i < m_lisAllEffect.Count; i++)
        {
            if (m_lisAllEffect[i].m_iId == id)
            {
                //真正的删除只在 m_lisAllEffect这张表里进行,实体那张只保持引用
                m_lisAllEffect[i].OnDelete();
                m_lisAllEffect.RemoveAt(i);
                i++;
            }
        }
        if (checkEntity)
        {
            foreach (EntityContainer vo in m_lisEntityEffect.Values)
            {
                for (int i = 0; i < vo.m_lisEffs.Count; i++)
                {
                    if (vo.m_lisEffs[i].m_iId == id)
                    {
                        vo.m_lisEffs.RemoveAt(i);
                        i++;
                    }
                }
            }
        }
    }

    public void DelEntityAllEffObj(int entityId) 
    {
        int id;
        if (m_lisEntityEffect.ContainsKey(entityId))
        {
            for (int i = 0; i < m_lisEntityEffect[entityId].m_lisEffs.Count; i++)
            {
                id = m_lisEntityEffect[entityId].m_lisEffs[i].m_iId;
                DelEffectObj(id, false);
                m_lisEntityEffect[entityId].m_lisEffs.RemoveAt(i);
                i++;
            }
        }
    }

    public void OnUpdate(float deltaTime) 
    {
        for (int i = 0; i < m_lisAllEffect.Count; i++)
        {
            m_lisAllEffect[i].OnUpdate(deltaTime);
        }
    }
}
